package com.example.librarymanagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.librarymanagement.dto.daily.BookAddListDto;
import com.example.librarymanagement.dto.daily.BookBorrowListDto;
import com.example.librarymanagement.dto.daily.BookReturnListDto;
import com.example.librarymanagement.entity.BookAddRecord;
import com.example.librarymanagement.entity.BorrowRecord;
import com.example.librarymanagement.util.DateFormatUtil;
import com.example.librarymanagement.util.EnumConst;
import com.example.librarymanagement.vo.BorrowRecordTopVo;
import com.example.librarymanagement.vo.DailyStatisticsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


/**
 * @author lzx
 */
@Api(tags = "每日盘店")
@RestController
@RequestMapping("/statisticsByDay")
public class DailyStatisticsController extends BaseController {

    @GetMapping("/add")
    @ApiOperation("每日新增书籍查询")
    @PreAuthorize("hasRole('admin')")
    public DailyStatisticsVo getBookDailyCreatement(@Validated BookAddListDto bookAddListDto) {
        if (bookAddListDto.getAddTime() == null) {
            bookAddListDto.setAddTime(DateFormatUtil.convertDateToString(new Date()));
        }
        DailyStatisticsVo dailyStatisticsVo = new DailyStatisticsVo();
        dailyStatisticsVo.setListData(bookAddRecordService.getDailyCreate(bookAddListDto));
        Long total = 0L;
        List<BookAddRecord> bookAddRecordAllList = bookAddRecordService.list(new QueryWrapper<BookAddRecord>()
                .eq("book_periodical", bookAddListDto.getBookPeriodical())
        );
        for (BookAddRecord bookAddRecord : bookAddRecordAllList) {
            total += bookAddRecord.getNumber();
        }
        dailyStatisticsVo.setTotal(total);
        Long daySize = 0L;
        List<BookAddRecord> bookAddRecordDayList = bookAddRecordService.list(new QueryWrapper<BookAddRecord>()
                .likeRight("add_time", bookAddListDto.getAddTime())
                .eq("book_periodical", bookAddListDto.getBookPeriodical())
        );
        for (BookAddRecord bookAddRecord : bookAddRecordDayList) {
            daySize += bookAddRecord.getNumber();
        }
        dailyStatisticsVo.setDaySize(daySize);
        return dailyStatisticsVo;
    }

    @GetMapping("/borrow")
    @ApiOperation("每日借书查询")
    @PreAuthorize("hasRole('admin')")
    public DailyStatisticsVo getBookDailyBorrow(@Validated BookBorrowListDto bookBorrowListDto) {
        if (bookBorrowListDto.getBorrowDate() == null) {
            bookBorrowListDto.setBorrowDate(DateFormatUtil.convertDateToString(new Date()));
        }
        DailyStatisticsVo dailyStatisticsVo = new DailyStatisticsVo();
        dailyStatisticsVo.setTotal((long) bookBorrowRecordService.count(new QueryWrapper<BorrowRecord>()
                .eq("book_periodical", bookBorrowListDto.getBookPeriodical())
        ));
        dailyStatisticsVo.setDaySize((long) bookBorrowRecordService.count(new QueryWrapper<BorrowRecord>()
                .eq("book_periodical", bookBorrowListDto.getBookPeriodical())
                .eq("borrow_date", bookBorrowListDto.getBorrowDate())
        ));
        return dailyStatisticsVo;
    }

    @GetMapping("/return")
    @ApiOperation("每日还书查询")
    @PreAuthorize("hasRole('admin')")
    public DailyStatisticsVo getBookDailyReturn(@Validated BookReturnListDto bookReturnListDto) {
        if (bookReturnListDto.getReturnDate() == null) {
            bookReturnListDto.setReturnDate(DateFormatUtil.convertDateToString(new Date()));
        }
        DailyStatisticsVo dailyStatisticsVo = new DailyStatisticsVo();
        dailyStatisticsVo.setTotal((long) bookBorrowRecordService.count(new QueryWrapper<BorrowRecord>()
                .eq("book_periodical", bookReturnListDto.getBookPeriodical())
                .eq("state", EnumConst.BORROW_RECORD_STATE_RETURN.getCode())
        ));
        dailyStatisticsVo.setDaySize((long) bookBorrowRecordService.count(new QueryWrapper<BorrowRecord>()
                .eq("book_periodical", bookReturnListDto.getBookPeriodical())
                .eq("return_date", bookReturnListDto.getReturnDate())
        ));
        return dailyStatisticsVo;
    }

    @GetMapping("/getBorrowBookTop")
    @ApiOperation("根据日期查询当日借阅图书前几名")
    @PreAuthorize("hasRole('admin')")
    public IPage<BorrowRecordTopVo> getTopFiveByBook(@Validated BookBorrowListDto bookBorrowListDto) {
        if (bookBorrowListDto.getBorrowDate() == null) {
            bookBorrowListDto.setBorrowDate(DateFormatUtil.convertDateToString(new Date()));
        }
        if (bookBorrowListDto.getBookPeriodical().equals(EnumConst.BOOK.getCode())) {
            return bookBorrowRecordService.getBorrowRecordTopByBook(bookBorrowListDto);
        } else {
            return bookBorrowRecordService.getBorrowRecordTopByPeriodical(bookBorrowListDto);
        }

    }
}
